
source('utils.R')
source('read_dataset.R')

donors = list(
  d12_inds = open_dataset(glue('final_long/cycle=2012')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r12_inds = open_dataset(glue('final_long/cycle=2012')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component),
  d16_inds = open_dataset(glue('final_long/cycle=2016')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r16_inds = open_dataset(glue('final_long/cycle=2016')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component),
  d20_inds = open_dataset(glue('final_long/cycle=2020')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r20_inds = open_dataset(glue('final_long/cycle=2020')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component)
)

# a

r12a = df12[component %chin% {
  open_dataset('final_long/cycle=2012') %>%
    filter(in_l2 == 1, in_cl == 1, total > 0, r_other_pcc > 0) %>%
    pull(component)},
  .(subsq_giver = component %chin% donors[['r12_inds']], nat_quant)
]

r12a = r12a[, .(sq = mean(subsq_giver)), nat_quant]
r12a[, class := 'Other Pres. Primary \'12 to Romney \'12']

r16a = df16[component %chin% {
  open_dataset('final_long/cycle=2016') %>%
    filter(in_l2 == 1, in_cl == 1, total > 0, r_other_pcc > 0) %>%
    pull(component)},
  .(subsq_giver = component %chin% donors[['r16_inds']], nat_quant)
]

r16a = r16a[, .(sq = mean(subsq_giver)), nat_quant]
r16a[, class := 'Other Pres. Primary \'16 to Trump \'16']

r20a = df16[component %chin% {
  open_dataset('final_long/cycle=2016/') %>%
    filter(in_l2 == 1, in_cl == 1, total > 0, r_other_pcc > 0) %>%
    pull(component)},
  .(subsq_giver = component %chin% donors[['r20_inds']], nat_quant)
]

r20a = r20a[, .(sq = mean(subsq_giver)), nat_quant]
r20a[, class := 'Other Pres. Primary \'16 to Trump \'20']

# b

r12b = df12[
  component %chin% {
    open_dataset('final_long/cycle=2012/') %>%
      filter(in_l2 == 1, in_cl == 1, total > 0, r_congress > 0) %>%
      pull(component)
    }, .(
      subsq_giver = component %chin% {
        open_dataset('final_long/cycle=2016/') %>%
          filter(in_l2 == 1, in_cl == 1, total > 0, r_congress > 0) %>%
          pull(component)
        },
      nat_quant
)]

r12b = r12b[, .(sq = mean(subsq_giver)), nat_quant]
r12b[, class := 'Congressional (R) \'12 to \'16']

r16b = df16[
  component %chin% {
    open_dataset('final_long/cycle=2016/') %>%
      filter(in_l2 == 1, in_cl == 1, total > 0, r_congress > 0) %>%
      pull(component)
  }, .(
    subsq_giver = component %chin% {
      open_dataset('final_long/cycle=2020/') %>%
        filter(in_l2 == 1, in_cl == 1, total > 0, r_congress > 0) %>%
        pull(component)
    },
    nat_quant
  )]

r16b = r16b[, .(sq = mean(subsq_giver)), nat_quant]
r16b[, class := 'Congressional (R) \'16 to \'20']

r20b = df12[
  component %chin% {
    open_dataset('final_long/cycle=2012/') %>%
      filter(in_l2 == 1, in_cl == 1, total > 0, r_congress > 0) %>%
      pull(component)
  }, .(
    subsq_giver = component %chin% {
      open_dataset('final_long/cycle=2020/') %>%
        filter(in_l2 == 1, in_cl == 1, total > 0, r_congress > 0) %>%
        pull(component)
    },
    nat_quant
  )]

r20b = r20b[, .(sq = mean(subsq_giver)), nat_quant]
r20b[, class := 'Congressional (R) \'12 to \'20']

save(r12a, r16a, r20a, r12b, r16b, r20b, file = 'summary_data/figA8.rda')
